﻿package Modules.MouseCursorModule
{
	import Modules.MainTimeLine;
	import Modules.Utilities;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;

	public class PlowTool extends Tool
	{
		//tempFarmObject is the semi transparent object that is used
		//to guide the player during object placement
		public var tempFarmObject:Bitmap;
		
		//OffsetX and Y are numbers to offset the tempfarmobject and the object to be placed
		//on the farm itself
		public var offsetX:Number;
		public var offsetY:Number;
		
		//Intercept determines if the object is being blocked from placement on the farm
		public var intercept:Boolean;
		
		//Constructor for mouse tool
		public function PlowTool()
		{
			//Call super function
			super();
			
			//Instantiate thetempfarm object
			//and make it semi transparent
			tempFarmObject = MainTimeLine.smallgrid;
			tempFarmObject.visible = true;
			tempFarmObject.alpha = 0.3;
			tempFarmObject.bitmapData.applyFilter(tempFarmObject.bitmapData, tempFarmObject.bitmapData.rect, new Point(), new ColorMatrixFilter(
				[0, 0.75, 0.75,0, //red
				0,0.75,0.75,0.75, //green
				0,0, 0.75,0.75,   //blue
				0.75, 0.75, 0.75,0.75,
				0,0,1, 0]));
			
			//Set an offset for the farm objects
			offsetX = tempFarmObject.width/2;
			offsetY = tempFarmObject.height/2;
		}
		
		//Function to call when the mouse is up
		override public function toolUp(event:MouseEvent):void{
			
			//Call super to check for click or hold
			super.toolUp(event);
			
			//If it is a click, perform the plow
			if(click){
				
				//Check if the object is being blocked from placement
				if(!intercept){
					
					/*OFFLOAD THIS ENTIRE PART TO THE AVATAR
					THIS PART IS ONLY HERE TO FACILITATE FASTER DEVELOPMENT*/
					
					//Ceate a new barren plowed plot of land
					var newObject:MovieClip = new CropDisplayObject();
					newObject.initObject();
					newObject.initPlowedCropObject();
					
					//Add the object onto the farm at the tempfarmObject's position
					//i.e. the position where the player puts the mouse at
					MainTimeLine.farmcontainer.addChild(newObject);
					newObject.x = tempFarmObject.x;
					newObject.y = tempFarmObject.y;
					
					//Update the object array in the farm itself
					MainTimeLine.farmcontainer.getFarmObjects();
					toolMove(event);

					/*END OFFLOAD*/
				}
			}else{
				
				//Exceeded Click time limit
				//Add any exceed click time limit functions here (so far none)
			}
		}
		
		//Function to check if the mouse is over the farm or not
		override public function toolOver(event:MouseEvent):void{
			
			//make the guide visible if the mouse is over the farm
			tempFarmObject.visible = true;
		}
		
		//Function for moving the tempfarmObject
		override public function toolMove(event:MouseEvent):void{
			
			//Move the tempfarmobject, center it to the mouse pointer
			tempFarmObject.x = event.localX-offsetX;
			tempFarmObject.y = event.localY-offsetY;
			
			//Offset when the mouse cursor is above a farm object
			if(!(event.target is Farm)){
				tempFarmObject.x += event.target.x;
				tempFarmObject.y += event.target.y;
			}
			
			//Gridding, round the object's position to the nearest "grid"
			//to facilitate aligning when placing objects
			tempFarmObject.x = Utilities.getNearestGridX(tempFarmObject.x);
			tempFarmObject.y = Utilities.getNearestGridY(tempFarmObject.y);
			
			//Collision detection
			intercept = !MainTimeLine.activeFarm.checkGridBounds(tempFarmObject.x, tempFarmObject.y);
			
			//Check the object against all objects on the farm (except for avatar)
			//And see if they intersect each other
			//Can be optimised but there is no pressing need to do that.
			var i:int = 0;
			var currentObject:MovieClip;
			
			if(!intercept){
				for(i=0; i< MainTimeLine.numFarmObjects; i++){
					
					currentObject = MainTimeLine.farmObjects[i];
					if(currentObject.grid != null){
						if(currentObject.grid.hitTest(new Point(currentObject.x, currentObject.y),255,tempFarmObject,new Point(tempFarmObject.x, tempFarmObject.y),	255)){
							intercept = true;
							break;
						}else{
							intercept = false;
						}
					}else{
						intercept = false;
					}
				}
			}
			
			//If intercept, make the object red, else it will be white
			if(intercept){
				tempFarmObject.bitmapData.applyFilter(tempFarmObject.bitmapData, tempFarmObject.bitmapData.rect, new Point(), new ColorMatrixFilter([1, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]));
			}else{
				tempFarmObject.bitmapData.applyFilter(tempFarmObject.bitmapData, tempFarmObject.bitmapData.rect, new Point(), new ColorMatrixFilter([0.75, 0.75, 0.75, 0, 0,0.75, 0.75, 0.75, 0, 0, 0.75, 0.75, 0.75, 0, 0, 0, 0, 0, 1, 0]));
			}
		}
	}
	
}